package com.lwlean.test.quartz;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.expression.ParseException;
import org.quartz.CronTrigger;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;


/**
 * Created by lean on 2017/1/20.
 */
@Configuration
public class SchedledConfiguration {

    @Value("${goip.sendTime}")
    private String sendTime;

    @Bean(name = "detailFactoryBean")
    public MethodInvokingJobDetailFactoryBean detailFactoryBean(ScheduledTasks scheduledTasks){
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();
        bean.setTargetObject (scheduledTasks);
        bean.setTargetMethod ("testFixedRate");
        bean.setConcurrent (false);
        return bean;
    }

    @Bean(name = "cronTriggerFactoryBean")
    public CronTriggerFactoryBean cronTriggerFactoryBean(MethodInvokingJobDetailFactoryBean detailFactoryBean){
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
        tigger.setJobDetail (detailFactoryBean.getObject ());
        try {
            tigger.setCronExpression (sendTime);
        } catch (ParseException e) {
            e.printStackTrace ();
        }
        return tigger;
    }

    @Bean
    public SchedulerFactoryBean schedulerFactory(CronTrigger[] cronTriggerFactoryBean){
        SchedulerFactoryBean bean = new SchedulerFactoryBean ();
        bean.setTriggers (cronTriggerFactoryBean);
        return bean;
    }
}


